<template>
  <div class="music-list">
    <div class="back" @click="goBack">
      <i class="icon-back" @click="$router.back()"></i>
    </div>
    <h1 class="title">{{ title }}</h1>
    <div class="bg-image" ref="bgImage" :style="bgImageStyle">
      <div class="play-btn-wrapper">
        <div v-show="songs.length > 0" class="play-btn">
          <i class="icon-play"></i>
          <span class="text" @click="playmusic()">随机播放全部</span>
        </div>
      </div>
      <div class="filter"></div>
    </div>
    <Scroll class="list" v-loading="loading" :probe-type="3" @scroll="onScroll">
      <div class="song-list-wrapper">
        <SongList :songs="songs" :rank="rank1" @select="selectItem"></SongList>
      </div>
    </Scroll>
  </div>
</template>

<script>
import { computed, ref } from 'vue'
import { useStore } from 'vuex'
import SongList from '@/components/song-list/song-list.vue'
import Scroll from '@/components/base/scroll/scroll.vue'

// const RESERVED_HEIGHT = 40

export default {
  name: 'music-list',
  props: {
    songs: {
      type: Array,
      default: () => []
    },
    title: String,
    pic: String,
    loading: {
      type: Boolean,
      default: true
    },
    rank: Boolean
  },
  components: {
    SongList,
    Scroll
  },
  setup (props) {
    const rank1 = computed(() => props.rank)
    const bgImage = ref(null)
    // 动态求图片高度
    const bgImageStyle = computed(() => {
      let height = '270px'
      let zIndex = 0
      let paddingTop = 0
      let translateZ = 0
      let scale = 1
      if (pagey.value >= 230) {
        zIndex = 10
        height = '30px'
        paddingTop = '0%'
        translateZ = 1
      }
      if (pagey.value < 0) {
        scale += Math.abs(pagey.value) / 270
      }

      return {
        backgroundImage: `url(${props.pic})`,
        height,
        zIndex,
        paddingTop,
        transform: `scale(${scale})translateZ(${translateZ}px)`
      }
    })
    const store = useStore()
    const playmusic = () => {
      store.dispatch('playmusic', { list: props.songs, index: 0 })
    }
    const pagey = ref(0)
    const onScroll = (pos) => {
      pagey.value = -pos.y
    }

    function selectItem ({ song, index, list }) {
      store.dispatch('playmusic', { list, index })
    }
    return { bgImageStyle, bgImage, onScroll, playmusic, rank1, selectItem }
  }
}
</script>

<style lang="scss" scoped>
.music-list {
  position: fixed;
  top: 0;
  bottom: 0;
  width: 100%;
  .back {
    position: absolute;
    top: 0;
    left: 6px;
    z-index: 20;
    transform: translateZ(2px);
    .icon-back {
      display: block;
      padding: 10px;
      font-size: $font-size-large-x;
      color: $color-theme;
    }
  }
  .title {
    position: absolute;
    top: 0;
    left: 10%;
    width: 80%;
    z-index: 20;
    transform: translateZ(2px);
    @include no-wrap();
    text-align: center;
    line-height: 40px;
    font-size: $font-size-large;
    color: $color-text;
  }
  .bg-image {
    position: relative;
    width: 100%;
    transform-origin: top;
    background-size: cover;
    .play-btn-wrapper {
      position: absolute;
      bottom: 30px;
      z-index: 10;
      width: 100%;
      .play-btn {
        box-sizing: border-box;
        width: 135px;
        padding: 7px 0;
        margin: 0 auto;
        text-align: center;
        border: 1px solid $color-theme;
        color: $color-theme;
        border-radius: 100px;
        font-size: 0;
      }
      .icon-play {
        display: inline-block;
        vertical-align: middle;
        margin-right: 6px;
        font-size: $font-size-medium-x;
      }
      .text {
        display: inline-block;
        vertical-align: middle;
        font-size: $font-size-small;
      }
    }
    .filter {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: rgba(7, 17, 27, 0.4);
    }
  }
  .list {
    position: absolute;
    bottom: 0;
    width: 100%;
    top: 270px;
    .song-list-wrapper {
      min-height: 300px;
      padding: 20px 30px;
      background: $color-background;
    }
  }
}
</style>
